/*
 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "se_napi_oma_reader.h"

#include "loghelper.h"
#include "se_napi_oma_common.h"
#include "se_napi_oma_session.h"

namespace OHOS {
namespace SE {
namespace KITS {
thread_local napi_ref NapiSEOmaReader::consRef_ = nullptr;

napi_value JSOmaReaderClassConstructor(napi_env env, napi_callback_info info)
{
    napi_value thisVar = nullptr;
    napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
    NapiSEOmaReader* omaJSClass = new NapiSEOmaReader();

    napi_wrap(
        env, thisVar, omaJSClass,
        [](napi_env env, void* data, void* hint) {
            NapiSEOmaReader* service = (NapiSEOmaReader*)data;
            if (service) {
                delete service;
                service = nullptr;
            }
        },
        nullptr,
        nullptr);
    return thisVar;
}

void NapiSEOmaReader::DefineSEOmaReaderJsClass(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        DECLARE_NAPI_FUNCTION("getName", NapiSEOmaReader::GetName),
        DECLARE_NAPI_FUNCTION("isSecureElementPresent", NapiSEOmaReader::IsSecureElementPresent),
        DECLARE_NAPI_FUNCTION("openSession", NapiSEOmaReader::OpenSession),
        DECLARE_NAPI_FUNCTION("closeSessions", NapiSEOmaReader::CloseSessions),
    };
    // define JS class SEOmaReader, JS_Constructor is the callback function
    napi_value constructor = nullptr;
    napi_define_class(env, "SEOmaReader", NAPI_AUTO_LENGTH, JSOmaReaderClassConstructor,
        nullptr, sizeof(desc)/sizeof(desc[0]), desc, &constructor);
    napi_create_reference(env, constructor, 1, &consRef_);
}

napi_value NapiSEOmaReader::GetName(napi_env env, napi_callback_info info)
{
    DebugLog("Enter function NapiSEOmaReader::GetName:");
    napi_value result = nullptr;
    napi_value thisVar = nullptr;
    size_t argc = ARGV_NUM_0;
    napi_value argv[] = {nullptr};
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
    NapiSEOmaCommon::CheckArgCountAndThrow(env, argc, ARGV_NUM_0);
    // TO-DO get name by call native method
    std::string name = "reader";
    int32_t status = SEErrorCode::ERR_NONE;

    if (!NapiSEOmaCommon::CheckSEStatusCodeAndThrow(env, status, "getName")) {
        return NapiSEOmaCommon::CreateUndefined(env);
    }
    napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &result);
    return result;
}

napi_value NapiSEOmaReader::IsSecureElementPresent(napi_env env, napi_callback_info info)
{
    DebugLog("Enter function NapiSEOmaReader::IsSecureElementPresent:");
    napi_value result = nullptr;
    napi_value thisVar = nullptr;
    size_t argc = ARGV_NUM_0;
    napi_value argv[] = {nullptr};
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
    NapiSEOmaCommon::CheckArgCountAndThrow(env, argc, ARGV_NUM_0);
    // TO-DO get IsSecureElementPresent by calling native method
    int32_t status = SEErrorCode::ERR_NONE;

    if (!NapiSEOmaCommon::CheckSEStatusCodeAndThrow(env, status, "isSecureElementPresent")) {
        return NapiSEOmaCommon::CreateUndefined(env);
    }
    napi_get_boolean(env, (status == SEErrorCode::ERR_NONE), &result);
    return result;
}

napi_value NapiSEOmaReader::OpenSession(napi_env env, napi_callback_info info)
{
    DebugLog("Enter function NapiSEOmaReader::OpenSession:");
    napi_value result = nullptr;
    napi_value thisVar = nullptr;
    size_t argc = ARGV_NUM_0;
    napi_value argv[] = {nullptr};
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
    NapiSEOmaCommon::CheckArgCountAndThrow(env, argc, ARGV_NUM_0);
    // TO-DO Open session by call native method, throw exception when fails
    int32_t status = 0;

    if (!NapiSEOmaCommon::CheckSEStatusCodeAndThrow(env, status, "openSession")) {
        return NapiSEOmaCommon::CreateUndefined(env);
    }

    // new instance of JS object
    napi_value constructor = nullptr;
    NAPI_CALL(env, napi_get_reference_value(env, NapiSEOmaSession::consRef_, &constructor));
    NAPI_CALL(env, napi_new_instance(env, constructor, argc, argv, &result));
    return result;
}

napi_value NapiSEOmaReader::CloseSessions(napi_env env, napi_callback_info info)
{
    DebugLog("Enter function NapiSEOmaReader::CloseSessions:");
    napi_value thisVar = nullptr;
    size_t argc = ARGV_NUM_0;
    napi_value argv[] = {nullptr};
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
    NapiSEOmaCommon::CheckArgCountAndThrow(env, argc, ARGV_NUM_0);
    // TO-DO shutdown service by call native method, throw exception when fails
    int32_t status = SEErrorCode::ERR_NONE;

    if (!NapiSEOmaCommon::CheckSEStatusCodeAndThrow(env, status, "closeSessions")) {
        //output error log
    }
    return NapiSEOmaCommon::CreateUndefined(env);
}
} // namespace KITS
} // namespace SE
} // namespace OHOS
